#include "BattStack.h"

#include "BatteryState.h"

//获取英文名称(主要供调试用)
QString CBattStack::GetNameOfStackState()
{
    return CBatteryState::GetName((int)this->usStackState);
}

//获取电池堆状态的中文utf8(主要供界面显示)
QString CBattStack::GetUtf8OfStackState()
{
    return CBatteryState::GetUtf8((int)this->usStackState);
}

//是不是禁止状态
bool CBattStack::IsForbidOfStackState()
{
    return CBatteryState::IsForbid((int)this->usStackState);
}

//是不是禁充状态
bool CBattStack::IsForbidChargeOfStackState()
{
    return CBatteryState::IsForbidCharge((int)this->usStackState);
}

//是不是禁访状态
bool CBattStack::IsForbidDischargeOfStackState()
{
    return CBatteryState::IsForbidDischarge((int)this->usStackState);
}

//是不是告警状态
bool CBattStack::IsAlarmOfStackState()
{
    return CBatteryState::IsAlarm((int)this->usStackState);
}

//是不是故障状态
bool CBattStack::IsFaultOfStackState()
{
    return CBatteryState::IsFault((int)this->usStackState);
}

//是不是正常状态
bool CBattStack::IsNormalOfStackState()
{
    return CBatteryState::IsNormal((int)this->usStackState);
}

//是不是选中了访客
bool CBattStack::IsSelectViewer()
{
    return 0 == usCurrentUserType;
}

//是不是选中了操作员
bool CBattStack::IsSelectOperator()
{
    return 1 == usCurrentUserType;
}

//是不是选中了工程师
bool CBattStack::IsSelectEngineer()
{
    return 2 == usCurrentUserType;
}

//选中访客
void CBattStack::SelectViewer()
{
    usCurrentUserType = 0;
}

//选中操作员
void CBattStack::SelectOperator()
{
    usCurrentUserType = 1;
}

//选中工程师
void CBattStack::SelectEngineer()
{
    usCurrentUserType = 2;
}

//获取用户的中文utf8(主要供界面显示)
QString CBattStack::GetUtf8OfUser()
{
    QString ret = "\xE8\xAE\xBF\xE5\xAE\xA2"; //访客

    if (IsSelectOperator())
    {
        ret = "\xE6\x93\x8D\xE4\xBD\x9C\xE5\x91\x98"; //操作员
    }
    else if (IsSelectEngineer())
    {
        ret = "\xE5\xB7\xA5\xE7\xA8\x8B\xE5\xB8\x88"; //工程师
    }

    return ret;
}

//是不是英文界面
bool CBattStack::IsEnglishUi()
{
    return (1 == this->usLang);
}

//是不是中文界面
bool CBattStack::IsChineseUi()
{
    return (0 == this->usLang);
}

//设置语言为英文
void CBattStack::SetToEnglishUi()
{
    this->usLang = 1;
}

//设置语言为中文
void CBattStack::SetToChineseUi()
{
    this->usLang = 0;
}

QString CBattStack::GetRtdbVersion()
{
    return QString("%1.%2.%3")
        .arg((int)uaRtdbVersMajor)
        .arg((int)uaRtdbVersMinor)
        .arg((int)uaRtdbVersRevision);
}

//分配主辅采集的电池簇组数量
void CBattStack::AssignDac()
{
    if (0 == usDacAssStrategy) //辅助采集策略
    {
        //0-全主采集,辅助不采集
        usDacSrvNum = usInstallBankNum;
        usDacAssNum = 0;
    }
    else if (1 == usDacAssStrategy)
    {
        //1-主辅采集各一半采集
        if (0 == usInstallBankNum % 2)
        {
            usDacSrvNum = (quint16)(usInstallBankNum / 2);
            usDacAssNum = usDacSrvNum;
        }
        else
        {
            //主采集少一个,辅助采集多一个
            usDacAssNum =  (quint16)(usInstallBankNum / 2);
            usDacSrvNum = usDacAssNum + 1;
        }
    }
    else if (usDacAssStrategy > 100 && usDacAssStrategy < 200)
    {
        //101-主采集只采集组1,其余辅助采集
        if (usInstallBankNum - (usDacAssStrategy - 100) > 0)
        {
            usDacSrvNum = usDacAssStrategy - 100;
            usDacAssNum = usInstallBankNum - usDacSrvNum;
        }
        else
        {
            usDacSrvNum = usInstallBankNum;
            usDacAssNum = 0;
        }
    }
    else if (200 == usDacAssStrategy)
    {
        //全辅助采集
        usDacSrvNum = 0;
        usDacAssNum = usInstallBankNum;
    }
    else
    {
        //全主采集
        usDacSrvNum = usInstallBankNum;
        usDacAssNum = 0;
    }
}

void CBattStack::InitCycleTimes()
{
    if (0 == usCycleTimes || usCycleTimes > 60000/*-1*/)
    {
        int iTempNum = (int)(fRatedVol * 10);

        if (12 == iTempNum || 20 == iTempNum || 60 == iTempNum
        || 120 == iTempNum || 160 == iTempNum || 480 == iTempNum)
        {
            usCycleTimes = 3000;
        }
        else if (23 == iTempNum)
        {
            usCycleTimes = 10000;
        }
        else if (32 == iTempNum)
        {
            usCycleTimes = 5000;
        }
        else if (36 == iTempNum)
        {
            usCycleTimes = 4000;
        }
        else
        {
            usCycleTimes = 5000;
        }
    }
}

//检查报警参数等级
void CBattStack::CheckAlarmParamLevel()
{
    if (ALARM_LEVEL3 != usAlarmParamLevel && ALARM_LEVEL4 != usAlarmParamLevel)
    {
        usAlarmParamLevel = ALARM_LEVEL3; //默认为3级
    }
}

void CBattStack::CheckAlarmSaveLevel()
{
    //报警保存等级:0保存所有等级;1只保存预警、告警和保护;2只保存告警和保护;3只保存保护
    if (usAlarmSaveLevel < 0)
    {
        usAlarmSaveLevel = 0;
    }
    else if (usAlarmSaveLevel > 3)
    {
        usAlarmSaveLevel = 3;
    }
}

bool CBattStack::IsStackVolValid()
{
    return (fStackVol < STACK_VOL_SAMPLE_INVALID_VALUE_UP)
        && (fStackVol > STACK_VOL_SAMPLE_INVALID_VALUE_DOWN);
}

bool CBattStack::IsStackVolInvalid()
{
    return !IsStackVolValid();
}

bool CBattStack::IsStackCurValid()
{
    // return (fStackCur < STACK_CUR_SAMPLE_INVALID_VALUE_UP)
    //     && (fStackCur > STACK_CUR_SAMPLE_INVALID_VALUE_DOWN);

    return (fStackCur < (BANK_CUR_SAMPLE_INVALID_VALUE_UP * usInstallBankNum))
        && (fStackCur > (BANK_CUR_SAMPLE_INVALID_VALUE_DOWN * usInstallBankNum));
}

bool CBattStack::IsStackCurInvalid()
{
    return !IsStackCurValid();
}

bool CBattStack::IsStackSocValid()
{
    return (fStackSoc < SOC_SAMPLE_INVALID_VALUE_UP)
        && (fStackSoc > SOC_SAMPLE_INVALID_VALUE_DOWN);
}

bool CBattStack::IsStackSocInvalid()
{
    return !IsStackSocValid();
}

bool CBattStack::IsStackSohValid()
{
    return (fStackSoh < SOH_SAMPLE_INVALID_VALUE_UP)
        && (fStackSoh > SOH_SAMPLE_INVALID_VALUE_DOWN);
}

bool CBattStack::IsStackSohInvalid()
{
    return !IsStackSohValid();
}

QDateTime CBattStack::GetTimeBranCurAbno(int iIndex)
{
    QDateTime dtBranCurAbno(QDate(usBranCurAbnoYear[iIndex], usBranCurAbnoMonth[iIndex], usBranCurAbnoDay[iIndex]),
                            QTime(usBranCurAbnoHour[iIndex], usBranCurAbnoMin[iIndex], usBranCurAbnoSec[iIndex]));

    return dtBranCurAbno;
}

void CBattStack::SetTimeBranCurAbno(int iIndex, const QDateTime& dt)
{
    //支路电流异常开始时间
    usBranCurAbnoYear[iIndex] = (quint16)(dt.date().year());
    usBranCurAbnoMonth[iIndex] = (quint16)(dt.date().month());
    usBranCurAbnoDay[iIndex] = (quint16)(dt.date().day());
    usBranCurAbnoHour[iIndex] = (quint16)(dt.time().hour());
    usBranCurAbnoMin[iIndex] = (quint16)(dt.time().minute());
    usBranCurAbnoSec[iIndex] = (quint16)(dt.time().second());
}
